//
//  MyTabBarController.m
//  OCDome
//
//  Created by 李含文 on 2020/7/30.
//  Copyright © 2020 李含文. All rights reserved.
//

#import "MyTabBarController.h"
#import "MyNavigationController.h"
#import "HWTabBar.h"
#import "MidViewController.h"
#import "NewMidViewController.h"

@interface MyTabBarController ()<UITabBarControllerDelegate>

@end

@implementation MyTabBarController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self setTabBarItem];
    
}
- (void)setUI {
    
    UITabBar *tabBar = [UITabBar appearance];
    UITabBarItem *tabBarItem = [UITabBarItem appearance];
    UIColor *normalColor = [UIColor lightGrayColor];
    UIColor *selectedColor = [UIColor redColor];
    
    // 设置默认颜色
    NSMutableDictionary *defaultdic = [[NSMutableDictionary alloc] init];
    defaultdic[NSForegroundColorAttributeName] = normalColor;
    [tabBarItem setTitleTextAttributes:defaultdic forState:UIControlStateNormal];
    // 设置选中颜色
    NSMutableDictionary *selecteddic = [[NSMutableDictionary alloc] init];
    selecteddic[NSForegroundColorAttributeName] = selectedColor;
    [tabBarItem setTitleTextAttributes:selecteddic forState:UIControlStateSelected];
    // 设置字体大小
    NSMutableDictionary *fontdic = [[NSMutableDictionary alloc] init];
    fontdic[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    [tabBarItem setTitleTextAttributes:fontdic forState:UIControlStateNormal];
    // 设置tab背景颜色
    [tabBar setBarTintColor:[UIColor whiteColor]]; // 不设置为黑色
    tabBar.translucent = NO;
    [tabBar setBackgroundColor:[UIColor whiteColor]];
//        tabBar.barStyle = UIBarStyleBlack; // 隐藏黑线
    
    if (@available(iOS 13.0, *)) { // iOS13 及以上
        self.tabBar.tintColor = selectedColor; //选中颜色
        self.tabBar.unselectedItemTintColor = normalColor; //默认颜色
    } else { // iOS13 以下
        
    }
    
    
}
- (void)setTabBarItem {
    [self setChildControllerWithTitle:@"首页" VCName:@"HomeViewController"];
    [self setChildControllerWithTitle:@"" VCName:@"MidViewController"];
    [self setChildControllerWithTitle:@"我的" VCName:@"MeViewController"];
    [self setCustomtabbar]; // 自定义tabbar 注释掉即常规
    self.delegate = self;
}
- (void)setChildControllerWithTitle:(NSString *)title VCName:(NSString *)name {
    UIViewController *vc = [[NSClassFromString(name) alloc] init];
    if (!vc) {return;}
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [[UIImage imageNamed:[NSString stringWithFormat:@"%@-未选", title]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@", title]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    if ([title isEqualToString:@"首页"]) { // 首页特殊处理
        CGFloat offset = 8.0;
        vc.tabBarItem.imageInsets = UIEdgeInsetsMake(offset, 0, -offset, 0);
        vc.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, 80);
    }
    MyNavigationController *nav = [[MyNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
}

// MARK: - 首页按钮相关
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    [self setSelected:tabBarController.selectedIndex];
}
- (void)setSelected:(NSInteger)index {
    if (index != 0) {
        for (UITabBarItem *item in self.tabBar.items) {
            if ([item.title isEqualToString:@"首页"]) {
                item.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
                item.titlePositionAdjustment = UIOffsetMake(0, 0);
            }
        }
    } else {
        CGFloat offset = 8.0;
        for (UITabBarItem *item in self.tabBar.items) {
            if ([item.title isEqualToString:@"首页"]) {
                item.imageInsets = UIEdgeInsetsMake(offset, 0, -offset, 0);
                item.titlePositionAdjustment = UIOffsetMake(0, 80);
            }
        }
    }
}
// MARK: - 中间按钮相关
- (void)setCustomtabbar{
    __weak typeof(self) weakself = self;
    HWTabBar *tabbar = [[HWTabBar alloc] initWithBlock:^{
        NSInteger index = weakself.childViewControllers.count/2;
        weakself.selectedIndex = index;
        [weakself setSelected:index];
    }];
    [self setValue:tabbar forKeyPath:@"tabBar"];
}

// MARK: 设置点击中间区域无效
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    //MARK: 因为用了自定义的导航控制器 必须这样实现
    if ([viewController isKindOfClass: HWNavigationController.class]) {
        HWNavigationController *nav = (HWNavigationController *)viewController;
        if ([nav.viewControllers.firstObject isKindOfClass:HWWrapViewController.class]) {
            HWWrapViewController *wp = nav.viewControllers.firstObject;
            if ([wp.rootViewController isMemberOfClass:[MidViewController class]])
                return NO;
        }
    }
    //MARK: 正常用这个
    if ([viewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nav = (UINavigationController *)viewController;
        if ([nav.viewControllers.firstObject isMemberOfClass:[MidViewController class]])
            return NO;
    }
    return YES;
}
@end
